import 'package:flutter_module/model/welfare_team_list_info.dart';
import 'package:flutter_module/page/welfare_ranking/welfare_ranking_list_item/welfare_ranking_list_item_state.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import '../../../common/base/logic/BasicListLogic.dart';
import '../../../common/http/ApiConstant.dart';
import '../../../common/http/YZHttp.dart';
import '../../../common/http/YZHttpUtils.dart';
import '../../../common/third/analytics/YZAnalytics.dart';
import '../../../model/welfare_all_teamlist_Info.dart';
import '../welfare_ranking_home_logic.dart';

class WelfareRankingListItemLogic extends BasicListLogic<WelfareTeamListInfo> {
  final state = WelfareRankingListItemState();
  late WelfareRankingHomeLogic welfareRankingHomeLogic;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    welfareRankingHomeLogic = Get.find<WelfareRankingHomeLogic>();
    state.countingType = welfareRankingHomeLogic.state.countingType.value;
    state.timeType = welfareRankingHomeLogic.state.timeType.value;
    if (state.countingType == 1) {
      YZAnalytics.track("S00079", "公益圈-公益时长榜", "上进社区", null);
    } else {
      YZAnalytics.track("S00084", "公益圈-人气榜", "上进社区", null);
    }
    requestRankingList(true, configNetWorkParmas());
    welfareRankingHomeLogic.state.countingType.listen(onCountingTypeChanged);
    welfareRankingHomeLogic.state.timeType.listen(onTimeTypeChanged);
  }

  @override
  Map<String, dynamic>? configNetWorkParmas() {
    return {'page': page, 'pageSize': PAGE_SIZE, 'timeType': state.timeType};
  }

  void onCountingTypeChanged(int newCountingType) {
    // 修改请求接口
    page = 1;
    controller.animateTo(
      0,
      duration: const Duration(milliseconds: 500),
      curve: Curves.easeInOut,
    );
    state.countingType = newCountingType;
    initTrack(state.timeType, state.countingType);
    requestRankingList(true, configNetWorkParmas());
  }

  void onTimeTypeChanged(int timeType) {
    // 时间类型 1=周 2=累计
    page = 1;
    controller.animateTo(
      0,
      duration: const Duration(milliseconds: 500),
      curve: Curves.easeInOut,
    );
    if (timeType == 1) {
      state.timeType = 1;
    } else {
      state.timeType = 2;
    }
    initTrack(state.timeType, state.countingType);
    requestRankingList(true, configNetWorkParmas());
  }

  void initTrack(int timeType, int countingType) {
    if (countingType == 1) {
      if (timeType == 1) {
        YZAnalytics.track("S00080", "公益圈-公益时长榜-月榜点击", "上进社区", null);
      } else {
        YZAnalytics.track("S00081", "公益圈-公益时长榜-累计榜点击", "上进社区", null);
      }
    } else {
      if (timeType == 1) {
        YZAnalytics.track("S00085", "公益圈-人气榜-周榜榜点击", "上进社区", null);
      } else {
        YZAnalytics.track("S00086", "公益圈-人气榜-累计榜点击", "上进社区", null);
      }
    }
  }

  void requestRankingList(bool isHeaderRefresh, Map<String, dynamic>? info) {
    SmartDialog.showLoading();
    if (state.countingType == 1) {
      state.apiUrl = api_welfare_time_list;
      HttpUtils.get(state.apiUrl, params: info, successCallBack: (data, total) {
        print('total->>>>>>> ${total}');
        dealRankingListDatas(data, isHeaderRefresh, total);
      }, errorCallBack: (e) {
        SmartDialog.dismiss();
        print('errorCallBack->>>>>>>');
        failRefresh(isHeaderRefresh, e);
      });
    } else {
      state.apiUrl = api_welfare_hot_list;
      HttpUtils.get(state.apiUrl, params: info, successCallBack: (data, total) {
        print('total->>>>>>> ${total}');
        dealRankingHotListDatas(data, isHeaderRefresh, total);
      }, errorCallBack: (e) {
        SmartDialog.dismiss();
        print('errorCallBack->>>>>>>');
        failRefresh(isHeaderRefresh, e);
      });
    }
  }

  @override
  void failRefresh(bool isHeaderRefresh, Exception e) {
    super.failRefresh(isHeaderRefresh, e);
    state.isShowMySelf = false;
  }

  @override
  void footerRetryRefresh() {
    refreshController.requestLoading();
  }

  @override
  void getnetworkdata(bool isHeaderRefresh, Map<String, dynamic>? info) {
    requestRankingList(isHeaderRefresh, configNetWorkParmas());
  }

  void dealRankingListDatas(data, bool isHeaderRefresh, int total) {
    SmartDialog.dismiss();
    WelfareTimeTeamListInfo allTeamList =
        WelfareTimeTeamListInfo.fromJson(data);
    if (allTeamList.myInfo != null) {
      state.isShowMySelf = true;
      state.myInfo.value = allTeamList.myInfo!;
    }
    List<WelfareTeamListInfo> items = allTeamList.infoList ?? List.empty();
    if (isHeaderRefresh) {
      dataList = items;
    } else {
      dataList.addAll(items);
    }
    successRefresh(isHeaderRefresh, items.length);
  }

  void dealRankingHotListDatas(data, bool isHeaderRefresh, int total) {
    SmartDialog.dismiss();
    WelfareHotTeamListInfo allTeamList = WelfareHotTeamListInfo.fromJson(data);
    if (allTeamList.myHotInfo != null) {
      state.isShowMySelf = true;
      state.myInfo.value = allTeamList.myHotInfo!;
    }
    List<WelfareTeamListInfo> items = allTeamList.hotInfoList ?? List.empty();
    if (isHeaderRefresh) {
      dataList = items;
    } else {
      dataList.addAll(items);
    }
    successRefresh(isHeaderRefresh, items.length);
  }
}
